package com.zhai.boot.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @package: com.zhai.boot.common
 * @project-name: zhai-boot
 * @description: 统一结果集（所有service层统一返回结果对象）
 * @author: Created by 翟凤玺
 * @create-datetime: 2018-7-10 11:50
 */
@Data
@ApiModel(value="结果集",description="统一返回结果对象")
public class ServiceResult<T> implements Serializable {

    @ApiModelProperty(value = "状态码")
    private String code;

    @ApiModelProperty(value = "信息")
    private String msg;

    @ApiModelProperty(value = "结果")
    private T result;

    /**
     * 自定义
     */
    public static <T> ServiceResult<T> defined(ServiceResult<T> serviceResult) {
        serviceResult.setCode(serviceResult.getCode());
        serviceResult.setMsg(serviceResult.getMsg());
        serviceResult.setResult(serviceResult.getResult());
        return serviceResult;
    }

    /**
     * 返回成功业务
     */
    public static <T> ServiceResult<T> success() {
        ServiceResult<T> serviceResult = new ServiceResult<T>();
        serviceResult.setCode(ServiceResultConstant.SYS_SUCCESS.getCode());
        serviceResult.setMsg(ServiceResultConstant.SYS_SUCCESS.getMsg());
        serviceResult.setResult(null);
        return defined(serviceResult);
    }

    /**
     * 返回成功业务
     */
    public static <T> ServiceResult<T> success(T result) {
        ServiceResult<T> serviceResult = new ServiceResult<T>();
        serviceResult.setCode(ServiceResultConstant.SYS_SUCCESS.getCode());
        serviceResult.setMsg(ServiceResultConstant.SYS_SUCCESS.getMsg());
        serviceResult.setResult(result);
        return defined(serviceResult);
    }

    /**
     * 返回失败业务
     */
    public static <T> ServiceResult<T> failure(ServiceResultConstant constant) {
        ServiceResult<T> serviceResult = new ServiceResult<T>();
        serviceResult.setCode(constant.getCode());
        serviceResult.setMsg(constant.getMsg());
        serviceResult.setResult(null);
        return defined(serviceResult);
    }

    /**
     * 返回失败业务
     */
    public static <T> ServiceResult<T> failure(ServiceResultConstant constant, T result) {
        ServiceResult<T> serviceResult = new ServiceResult<T>();
        serviceResult.setCode(constant.getCode());
        serviceResult.setMsg(constant.getMsg());
        serviceResult.setResult(result);
        return defined(serviceResult);
    }

    /**
     * 返回失败业务
     */
    public static <T> ServiceResult<T> failure(String code, String msg) {
        ServiceResult<T> serviceResult = new ServiceResult<T>();
        serviceResult.setCode(code);
        serviceResult.setMsg(msg);
        serviceResult.setResult(null);
        return defined(serviceResult);
    }

    /**
     * 返回结果是否成功
     */
    public boolean isSuccess() {
        return this.code.equals(ServiceResultConstant.SYS_SUCCESS.getCode());
    }

    /**
     * 返回结果是否失败
     */
    public boolean isFailure() {
        return !this.code.equals(ServiceResultConstant.SYS_SUCCESS.getCode());
    }

}
